# -*- coding: utf-8 -*-
import requests
import datetime
import logging
import re
import json
import time
import csv

# 全局Session
session = requests.session()
session.headers.update({
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
})

# 正则
RE_ITEMS_LIST = re.compile("g_page_config = (.*);")
RE_ITEMS_SALES = re.compile("(.*?)人收货")
RE_HTMLS_TAG = re.compile("<.*?>")
RE_LINK_TAOBAO = re.compile("item.taobao.com")
RE_LINK_TIMALL = re.compile("detail.tmall.com")


# 商品
class Item(object):

    def __init__(self, kargs):
        self.nid = kargs["nid"]
        self.category = kargs["category"]
        self.pid = kargs["pid"]
        self.title = kargs["raw_title"]
        self.pic_url = "https:" + kargs["pic_url"]
        self.detail_url = "https:" + kargs["detail_url"]
        self.price = kargs["view_price"]
        self.fee = kargs["view_fee"]
        self.location = kargs["item_loc"]
        self.sales = re.search(RE_ITEMS_SALES, kargs["view_sales"]).group(1)
        self.comment_count = kargs["comment_count"]
        self.user_id = kargs["user_id"]
        self.shop_name = kargs["nick"]
        self.spec = ""
        for i in kargs["icon"]:
            if i["innerText"] == "度量单位":
                self.spec = re.sub(RE_HTMLS_TAG, "", i["html"])

    def list(self):
        return [
            self.title,
            self.spec,
            self.price,
            self.fee,
            self.location,
            self.sales,
            self.comment_count,
            self.shop_name,
            self.detail_url
        ]


# 天猫商品
class TmallItem(Item):

    def __init__(self, kargs):
        Item.__init__(self, kargs)

    def getComment_raw(self, page=1):
        resp = session.get(
            url="https://rate.tmall.com/list_detail_rate.htm",
            params={
                "itemId": self.nid,
                "sellerId": self.user_id,
                "order": "3",
                "currentPage": page
            }
        ).text
        return json.loads("{" + resp.strip() + "}")

    def getComments(self, pages=1):
        now = 1
        comments = []
        if pages > 99:
            pages = 99
        while(now <= pages):
            resp = self.getComment_raw(now)
            if pages > resp["rateDetail"]["paginator"]["lastPage"]:
                pages = resp["rateDetail"]["paginator"]["lastPage"]
            for i in resp["rateDetail"]["rateList"]:
                comments.append({
                    "UserNick": i["displayUserNick"],
                    "date": i["rateDate"],
                    "id": i["id"],
                    "content": re.sub(RE_HTMLS_TAG, "", i["rateContent"]),
                    "append": i["appendComment"]["content"] if i["appendComment"] else "无"
                })
            now += 1
            time.sleep(1)
        return comments


# 淘宝商品
class TaobaoItem(Item):

    def __init__(self, kargs):
        Item.__init__(self, kargs)

    def getComment_raw(self, page=1):
        resp = session.get(
            url="https://rate.taobao.com/feedRateList.htm",
            params={
                "auctionNumId": self.nid,
                "userNumId": self.user_id,
                "currentPageNum": page,
                "pageSize": 20,
                "orderType": "sort_weight"
            }
        ).text.strip()[1:-1]
        return json.loads(resp)

    def getComments(self, pages=1):
        now = 1
        comments = []
        if pages > 99:
            pages = 99
        while(now <= pages):
            resp = self.getComment_raw(now)
            if pages > resp["maxPage"]:
                pages = resp["maxPage"]
            for i in resp["comments"]:
                comments.append({
                    "UserNick": i["user"]["nick"],
                    "date": i["date"],
                    "id": i["rateId"],
                    "content": re.sub(RE_HTMLS_TAG, "", i["content"]),
                    "append": i["append"]["content"] if i["append"] else "无"
                })
            now += 1
            time.sleep(1)
        return comments


def search_raw(text, page=1):
    resp = session.get(
        url="https://s.taobao.com/search",
        params={
            "q": text,
            "imgfile": "",
            "js": 1,
            "stats_click": "search_radio_all:1",
            "initiative_id": "staobaoz_" + datetime.datetime.now().strftime("%Y%M%d"),
            "ie": "utf8",
            "sort": "sale-desc",
            "s": 44 * (page - 1)
        }
    )
    goods_list = json.loads(re.search(RE_ITEMS_LIST, resp.text).group(1))
    return goods_list


# 搜索
def search(text, pages=1):
    now = 1
    items = []
    if pages > 100:
        pages = 100
    while(now <= pages):
        resp = search_raw(text, now)
        logging.info("抓取商品 %s 第 %d 页" % (text, now))
        if pages > resp["mods"]["pager"]["data"]["totalPage"]:
            pages = resp["mods"]["pager"]["data"]["totalPage"]
        for i in resp["mods"]["itemlist"]["data"]["auctions"]:
            if re.search(RE_LINK_TAOBAO, i["detail_url"]):
                tmp = TaobaoItem(i)
            else:
                tmp = TmallItem(i)
            items.append(tmp)
        now += 1
        time.sleep(1)
    return items


# 将搜索结果保存到csv
def save_items_to_csv(filename, items):
    with open(filename, "w", encoding="gb2312", errors="ignore") as file:
        writer = csv.writer(file)
        writer.writerow(["商品名称", "规格", "单价", "运费", "发货地", "销量", "评价数", "店名", "详细链接"])
        for i in items:
            writer.writerow(i.list())


# 将评论保存到csv
def save_comments_to_csv(filename, comments):
    with open(filename, "w", encoding="gb2312", errors="ignore") as file:
        writer = csv.writer(file)
        writer.writerow(["用户名", "内容", "日期", "追加评论"])
        for i in comments:
            print([i["UserNick"], i["content"], i["date"], i["append"]])
            writer.writerow([i["UserNick"], i["content"], i["date"], i["append"]])
